package com.itHuangTao.tank.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.itHuangTao.tank.Config;
import com.itHuangTao.tank.GameType;

import java.util.List;

public class BulletBrickHandler extends CollisionHandler {
    public BulletBrickHandler() {
        super(GameType.BULLET, GameType.BRICK);
    }

    @Override
    protected void onCollision(Entity bullet, Entity brick) {
        //过滤器判断碰撞
        //先查找与子弹碰撞的实体
        List<Entity> es = FXGL.getGameWorld().getEntitiesFiltered(entity ->
                bullet.isColliding(entity)
                        &&
                        (
                           entity.isType(GameType.STONE)
                        || entity.isType(GameType.GREENS)
                        || entity.isType(GameType.BRICK)
                        )
        );
        //子弹是否需要清除
        boolean isRemoveBullet=false;
        for (int i = 0; i < es.size(); i++) {
            Entity entity = es.get(i);
            GameType type = (GameType) entity.getType();
            switch (type) {
                case BRICK -> {
                    entity.removeFromWorld();
                    isRemoveBullet=true;
                }
                case STONE -> {
                    int value=bullet.getInt("level");
                    if(value==Config.TANK_MAX_LEVEL){
                        entity.removeFromWorld();
                    }
                    isRemoveBullet=true;
                }
                case GREENS -> {
                    int value=bullet.getInt("level");
                    if(value==Config.TANK_MAX_LEVEL){
                        entity.removeFromWorld();
                    }
                }
            }
        }
        if (isRemoveBullet) {
            FXGL.spawn("explode",bullet.getCenter()
                    .subtract(Config.CELL_SIZE/2.0,Config.CELL_SIZE/2.0));//在物体中心渲染爆炸
            bullet.removeFromWorld();
        }
    }
}
